--[[			Chicchai
]]--	by Lolzen & Cargor (EU-Nozdormu)

-- Configuration
local maxHeight = 120				-- How high the chat frames are when maximized
local animTime = 0.3				-- How lang the animation takes (in seconds)
local minimizeTime = 10				-- Minimize after X seconds
local MaximizeCombatLog = true		-- When the combat log is selected, it will be maximized
local MaximizeOnEnter = true		-- Maximize when entering chat frame, minimize when leaving
--local NoInstantMinimize = true				-- Wait [minimizeTime] seconds after leaving the chat frame before minimizing
local LockInCombat = true			-- Do not maximize in combat

FCF_ValidateChatFramePosition = function() return nil end	-- Move chat frames completely to the bottom

local events = {					-- Events which maximize the chat
--	"CHAT_MSG_CHANNEL",
--	"CHAT_MSG_OFFICER",
	"CHAT_MSG_BG_SYSTEM_ALLIANCE",
	"CHAT_MSG_BG_SYSTEM_HORDE",
	"CHAT_MSG_BG_SYSTEM_NEUTRAL",
	"CHAT_MSG_BATTLEGROUND",
	"CHAT_MSG_BATTLEGROUND_LEADER",
	"CHAT_MSG_PARTY",
	"CHAT_MSG_RAID",
	"CHAT_MSG_RAID_LEADER",
	"CHAT_MSG_GUILD",
--	"CHAT_MSG_SAY",
--	"CHAT_MSG_SYSTEM",
	"CHAT_MSG_WHISPER",
--	"CHAT_MSG_WHISPER_INFORM",
--	"CHAT_MSG_LOOT",
--	"CHAT_MSG_YELL",
}

local chatFrames = {
	["ChatFrame1"] = true,
}
-- Configuration End
-- Do not change anything under this line except you know what you're doing (:


local minHeight, percent, line
local select = select
local Chicchai = CreateFrame("Frame")

-- Hook functions
local SizeCombatLog = function(button)
	if(this:GetID()==2) then
		Chicchai:Maximize()
		Chicchai.Frozen = true
	else
		Chicchai.Frozen = nil
	end
end

local FCF_UpdateHook = function()
	for k,v in pairs(chatFrames) do
		if(MouseIsOver(_G[k]) and not Chicchai.wasOver) then
			Chicchai.wasOver = true
			Chicchai:Maximize()
		elseif(Chicchai.wasOver and not MouseIsOver(_G[k])) then
			Chicchai.wasOver = nil
			if(NoInstantMinimize and not Chicchai.Frozen) then
				Chicchai.TimeToFade = minimizeTime
			else
				Chicchai:Minimize()
			end
		end
	end
end

-- Core functions
function Chicchai:New()
	for _, event in pairs(events) do
		self:RegisterEvent(event)
	end
	events = nil
	self.wasOver = {}
	self:SetScript("OnUpdate", self.Update)
	if(LockInCombat) then
		self:SetScript("OnEvent", function(self) if(not UnitAffectingCombat("player")) then self:Maximize() end end)
	else
		self:SetScript("OnEvent", self.Maximize)
	end
	if(MaximizeCombatLog) then hooksecurefunc("FCF_Tab_OnClick", SizeCombatLog) end
	if(MaximizeOnEnter) then hooksecurefunc("FCF_OnUpdate", FCF_UpdateHook) end
end

function Chicchai:Update(elapsed)
	if(self.TimeToFade) then self.TimeToFade = self.TimeToFade - elapsed end
	if(self.TimeToFade and self.TimeToFade <= 0) then
		self:Minimize()
	end

	if(not self.Animate) then return nil end
	self.TimeRunning = self.TimeRunning + elapsed
	percent = self.TimeRunning/animTime
	if(self.Animate < 0) then percent = 1 - percent end

	if(percent >= 1) then
		percent = 1
		self.Animate = nil
		self.IsUp = true
	elseif(percent <= 0) then
		percent = 0
		self.Animate = nil
		self.IsUp = nil
	end

	for k,v in pairs(chatFrames) do
		line = select(2, _G[k]:GetRegions())
		if(line) then
			minHeight = line:GetHeight()+2.5
		else
			minHeight = select(2, _G[k]:GetFont())+2.5
		end
		_G[k]:SetHeight(minHeight+(maxHeight-minHeight)*percent)
	end
end

function Chicchai:Maximize()
	if(self.IsUp) then return nil end
	if(not self.Animate or self.Animate < 0) then
		self.Animate = 1
		self.TimeRunning = 0
	end
	self.TimeToFade = minimizeTime
end

function Chicchai:Minimize()
	if(self.Frozen or self.wasOver) then return nil end
	self.TimeToFade = nil
	self.Animate = -1
	self.TimeRunning = 0	
end

Chicchai:New()
_G['Chicchai'] = Chicchai